Our objective is to see how to create a document using R.
The document will combine: - text - code - R output
The document will be in html, so we can use both as a text document and for presentation.
Specialized formats are also available, but this is a flexible method.
Types of commonly used files
owid_ies <- read.csv("~/Documents/R/math_R/owid_ies.csv")
Data is from the Our World in Data website, publicly available and easily downloadable.
We look at the structure of the data with str().
str(owid_ies)
## 'data.frame': 539 obs. of 6 variables:
## $ iso_code : chr "BGD" "BGD" "BGD" "BGD" ...
## $ continent : chr "Asia" "Asia" "Asia" "Asia" ...
## $ location : chr "Bangladesh" "Bangladesh" "Bangladesh" "Bangladesh" ...
## $ date : chr "2020-03-03" "2020-03-09" "2020-03-15" "2020-03-21" ...
## $ new_cases_smoothed_per_million : num NA NA 0.002 0.019 0.024 ...
## $ new_deaths_smoothed_per_million: num NA NA 0 0.002 0.003 0.001 0.012 0.025 0.054 0.047 ...
We change location variable to a factor.
owid_ies$location <- factor(owid_ies$location)
str(owid_ies$location)
## Factor w/ 6 levels "Bangladesh","Brazil",..: 1 1 1 1 1 1 1 1 1 1 ...
We load packages.
library(ggplot2) # for plotting
library(dplyr) # for working with data
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(scales) # for working with scales
library(lubridate) # for dates
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(ggthemes) # for themes of graph
We work with dates.
owid_ies$date <- ymd(owid_ies$date)
datebreaks <- seq(as.Date("2020-01-01"),
as.Date("2021-07-01"),
by = "6 month")
We plot the figure.
gg3 <-
ggplot(owid_ies, aes(x = date,
y = new_cases_smoothed_per_million,
group = location)) +
geom_line() +
facet_wrap(~location, scales = "free_y",
ncol = 2) +
scale_x_date(breaks = datebreaks,
labels = date_format("%Y %b")) +
theme(axis.text.x = element_text(
angle = 30)) +
xlab("") +
ggtitle("COVID-19 cases") +
theme_fivethirtyeight(base_size = 9)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly(gg3)
Different countries experienced COVID-19 in waves.